<?php
namespace app\admin\controller;

use app\admin\model\Role as RoleModel;
use app\admin\model\Accesses;
use app\admin\model\Admin;
use think\Request;
use think\Config;
use think\Cache;
use think\Validate;

class Role extends Base
{
	/*
	 *	角色列表
	*/
	public function index()
	{
		$map = [];
		if (!empty($this->request->param('status'))) {
            $map['status'] = $this->request->param('status');
        }
		$list = RoleModel::where($map)->field('id, role_name, status, id as ucount')->select();
		$menu = self::getMenuArr();

		return view('index', compact('list', 'menu'));
	}

	/*
	 *	添加角色
	*/
	public function add()
	{
		if(Request::instance()->isPost()){
			$data = $this->request->param();

			// $aa = implode(',',$data['role_accesses']);
			$validate = new Validate([
	            ['role_name', 'require', '请输入角色名称'],
	            ['status', 'require', '请选择角色状态'],
	            ['role_accesses', 'require', '请勾选权限'],
	        ]);
	        if (!$validate->check($data)) {
	            return error($validate->getError());
	        }

	        $res = RoleModel::create([
	        	'role_accesses' => implode(',', $data['role_accesses']),
	        	'role_name' => $data['role_name'],
	        	'status' => $data['status']
	        ]);

	        if($res){
	        	logger(2, '添加角色：'.$data['role_name'].",并且赋予权限");

	        	return success(['url'=>"/admin/role/index"], '添加成功');
	        }

	        return error('添加失败');
		}

		$accArr = [];
		$accesses = Accesses::where('pid', 0)
			->order('sort ASC')
			->field('id, name, sign')
			->select();

		foreach ($accesses as $key => $value) {
			$accesses[$key]['second'] = Accesses::where('pid', $value['id'])
				->order('sort ASC')
				->field('id, name, sign')
				->select();
		}

		// dd($accesses);
		$menu = self::getMenuArr();

		return view('add', compact('accesses', 'menu'));
	}

	/*
	 *	编辑角色
	*/
	public function edit()
	{
		if(Request::instance()->isPost()){
			$data = $this->request->param();

			// $aa = implode(',',$data['role_accesses']);
			$validate = new Validate([
	            ['role_name', 'require', '请输入角色名称'],
	            ['status', 'require', '请选择角色状态'],
	            ['role_accesses', 'require', '请勾选权限'],
	        ]);
	        if (!$validate->check($data)) {
	            return error($validate->getError());
	        }

	        $update = array(
	        	'role_name' => $data['role_name'],
	        	'status' => $data['status'],
	        	'role_accesses' => implode(',', $data['role_accesses']),
	        );
	        // dd($update);

	        $res = RoleModel::where('id', $data['id'])->update($update);

	        if($res || $res !== false){
	        	logger(2, '编辑角色/权限：'.$data['role_name']);
	        	return success(['url'=>"/admin/role/index"], '更新成功');
	        }

	        return error('更新失败');
		}

		$id = $this->request->param('id');
		$detail = RoleModel::find($id);

		$accArr = [];
		$accesses = Accesses::where('pid', 0)
			->order('sort ASC')
			->field('id, name, sign')
			->select();

		foreach ($accesses as $key => $value) {
			$accesses[$key]['second'] = Accesses::where('pid', $value['id'])
				->order('sort ASC')
				->field('id, name, sign')
				->select();
		}

		// dd($accesses);
		$menu = self::getMenuArr();

		return view('edit', compact('accesses', 'detail', 'menu'));
	}



	/**
	*	删除
	*/
	public function del()
	{
		$id = $this->request->param('id');
		if(empty(trim($id))){
			return error('ID不能为空！');
		}

		//判断当前角色有没有关联管理员账户
		$info = Admin::where('role_id', $id)->count();
		if($info){
			return error('不允许删除');
		}

		$res = RoleModel::find($id);

		if(!$res){
			return error('数据不存在或被删除');
		}
		logger(2, '删除角色：'.$res['role_name']);
		
		$res->delete();

		return success(1, '删除成功！');
	}
}
